import './db_provider.dart';
import '../model/base.dart';


/// 通用增删改查sql
class Sql extends BaseModel {
  final String tableName;
  // 构造器
  Sql.setTable(this.tableName) : super(DbProvider.db);

  /// 根据条件查询某张表中符合条件的数据
  Future<List> getBySql(String where, List<dynamic> whereArgs,
      {String orderBy, int limit, int offset}) async {
    return await this.query(tableName,
        where: where,
        whereArgs: whereArgs,
        orderBy: orderBy,
        limit: limit,
        offset: offset);
  }

  /// 查询某张表中所有数据
  Future<List> get() async {
    return await this.query(tableName);
  }

  /// 改
  Future<int> rawUpdate(String sql, [List<dynamic> arguments]) async {
    return await this.db.rawUpdate('UPDATE $tableName SET $sql', arguments);
  }
  /// 增
  Future<int> rawInsert(String valueNames, [List<dynamic> arguments]) async {
    return await this
        .db
        .rawInsert('INSERT INTO $tableName$valueNames', arguments);
  }
  /// 删
  Future<int> rawDelete(String wheres, [List<dynamic> arguments]) async {
    return await this
        .db
        .rawDelete('DELETE FROM $tableName WHERE $wheres', arguments);
  }
  /// 获取表名
  String getTableName() {
    return tableName;
  }

  /// 删除某张表中所有数据
  Future<int> deleteAll() async {
    return await this.db.delete(tableName);
  }


}